package xuan.codepractice;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class StockMaxValue implements Runnable{
	public TimeMeter m_meter = null;
	public StockMaxValue(TimeMeter meter)
	{
		m_meter = meter;
	}
	/*
	<T extends Comparable<T>> void SingleSellProfit(T array[],T result1, T result2)
	{
		T maxV = array[0];
		T minV = array[0];
		T maxResult = array[0];
		result1 = array[0];
		for (int i = 0; i < array.length; i++) {
			if (array[i].compareTo(maxV) > 0)
			{
				maxV = array[i];
				result2 = maxV;
			}else
			{
				result1 = array[i];
				result2 = array[i];
				maxV = array[i];
				minV = array[i];
			}
		}
		//return maxResult;
	}
	*/
	Integer SingleSellProfit(Integer array[])
	{
		Integer maxV = array[0];
		Integer minV = array[0];
		Integer maxResult = -1;
		
		for (int i = 0; i < array.length; i++) {
			if (array[i] > (maxV))
			{
				maxV = array[i];
				maxResult = maxV - minV;
			}else
			{
				maxV = array[i];
				minV = array[i];
			}
		}
		return maxResult;
	}
	
	public void run()
	{
		int testdata[] = {16,4,2,1};
		List<Integer> listHolder = new LinkedList<Integer>();
		Random random = new Random();
		for (int i = 0; i < 100000; i++) {
			listHolder.add(Integer.valueOf(random.nextInt(20)));			
		}
		Integer[] r = new Integer[0];
		r = listHolder.toArray(r);
		
		
		r = AlgorithmLoader.toIntegerArray(testdata);
		m_meter.start();
		Integer result = SingleSellProfit(r);
		System.out.println("---------------------------------");
		System.out.println(result);
		System.out.println("---------------------------------");
		m_meter.stop();
	}
}